package com.blobfish.fiction.book;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.blobfish.fiction.book.constant.BookMessageEnum;
import com.blobfish.fiction.book.dto.BookDto;
import com.blobfish.fiction.book.dto.BookTableDto;
import com.blobfish.fiction.book.service.impl.BookServiceImpl;
import com.blobfish.fiction.book.validator.AddBookValidator;
import com.blobfish.fiction.book.validator.DownloadTxtValidator;
import com.blobfish.fiction.common.base.exception.GlobalErrorException;
import com.blobfish.fiction.common.model.Book;
import com.blobfish.fiction.common.base.controller.BaseController;
import com.blobfish.fiction.common.plugin.ShowcaseWebsocketPlugin;
import com.blobfish.fiction.common.tio.websocket.Const;
import com.blobfish.fiction.common.tio.websocket.ShowcaseServerConfig;
import com.blobfish.fiction.common.util.DownloadUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.paragetter.Para;
import com.jfinal.log.Log;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;

import java.io.File;
import java.util.Date;

/**
 * 小说控制器
 * @author huangbo
 */
public class BookController extends BaseController<Book, BookDto> {
    private static final Log log = Log.getLog(BookController.class);
    private static final String SEARCH_URL_FMT = "https://m.wujixiaoshuo.com/SearchBook.php?keyword={}";

    @Inject
    BookServiceImpl bookService;
    @Inject
    BookChapterService bookChapterService;

    /**
     * 本地搜索小说方法
     * @param qmParam
     */
    public void searchFromLocal(@Para("") BookTableDto qmParam) {
        WsResponse wsResponse = WsResponse.fromText("{message:'点击搜索小说'}", ShowcaseServerConfig.CHARSET);
        // Tio.sendToGroup(ShowcaseWebsocketPlugin.serverGroupContext, Const.GROUP_ID, wsResponse);
        Tio.sendToUser(ShowcaseWebsocketPlugin.serverGroupContext, "admin", wsResponse);
        renderJson(bookService.searchFromLocal(qmParam));
    }

    /**
     * 外网根据名称搜索小说方法
     * @author huangbo
     */
    public void searchFromWeb(@Para("") BookTableDto qmParam) {
        String name = qmParam.getName();
        log.info(StrUtil.format("根据名称【{}】搜索小说", name));
        renderJson(bookService.searchFromWeb(qmParam));
        log.info("搜索完毕!");
    }

    /**
     * 添加到本地并下载
     * @author huangbo
     */
    @Before(AddBookValidator.class)
    public void addAndDownload(@Para("")Book book) {
        book.setAddTime(new Date());
        String bookNo = bookService.insert(book);
        if (bookService.isDownloaded(bookNo)) {
            throw new GlobalErrorException(BookMessageEnum.BOOK_HAS_DOWNLOADED);
        }
        // 开始下载开说
        DownloadUtil.getInstance().addTask(bookNo);
        renderJson(renderSuccess("添加下载任务成功!", true));
    }

    /**
     * 下载txt方法
     */
    @Before(DownloadTxtValidator.class)
    public void downloadTxt() {
        String bookNo = getPara(0);
        String functionPath = bookService.buildBook(bookNo);
        if (Validator.isNotEmpty(functionPath)) {
            renderFile(new File(functionPath));
        } else {
            renderError(500);
        }
    }
}
